/**
 * Created by zhanglihe on 15/9/9.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function( $, common, ready, template, Collection, Model, dialog ) {

    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
        },

        fetch: function() {
            var that = this;
            that.collection = new Collection({
                container: 'data-list',
                template: 'new-data-item',
                url: _global.url.api + 'shipping',
                condition: {limit: 10, offset: 0},
                model: {
                    tag_name: 'tr',
                    url: {
                        post: _global.url.api + 'shipping/{id}',
                        put: _global.url.api+'shipping/{id}',
                        copy: _global.url.api+'shipping/copy/{id}',
                    },
                    event: {
                        '.item-del:click': 'del',
                        '.item-copy:click': 'copy'
                    },
                    del: function(e) {
                        e.preventDefault();
                        var _this = this;

                        dialog({
                            content: '确定要删除吗?',
                            ok: function() {
                                if(!_this.post()) {
                                    console.log('put false');
                                    return false;
                                }
                                that.collection.reload();
                            } ,cancel: function() {}
                        }).show(e.target);
                    },
                    copy: function(e) {
                        e.preventDefault();
                        var _this = this;

                        dialog({
                            content: '确定要继续吗?',
                            ok: function() {
                                var u = _this.get_url('copy');
                                $.ajax({
                                    url: u,
                                    type: "put",
                                    dataType: 'json',
                                    success: function(d) {
                                        if (d.code == 0) {
                                            that.collection.reload();
                                        } else {
                                            Alert({"msg": d.msg, "type":"error"});
                                        }
                                    },
                                    error: function() {
                                        Alert({"msg":"操作失败", "type":"error"});
                                    }
                                });
                            } ,cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                    that.event();
                }
            });
            that.collection.fetch();
        },

        event: function(e) {
            var _this = this;
        }
    };

    new index();

});